﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using CRM.Entity;

using CRM.BLL;
using DP.Web.UI.Controls;
using DP.Web.UI;
using DP.Common;

namespace CRM.Web.DealerManage
{
    public partial class List : Common.PageBase
    {

        public string ModifyUrl = "Modify.aspx?DEALERID={0}";
        T_DEALER_BLL bll = new T_DEALER_BLL();
        //SqlServerDAL<T_DEALER_Entity> dal = new SqlServerDAL<T_DEALER_Entity>();

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            PageHelper.SetPrePageUrl(this.Page);
            gvList.PageSize = StaticConstant.PageSize;
            PageHelper.SetPopCalender(this.Page, txtCREATETIMEBegin);
            PageHelper.SetPopCalender(this.Page, txtCREATETIMEEnd);

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                GetUrlQueryToControl();
                gvDataBinding();
            }
        }

        #region 页面初始化数据绑定



       







        #endregion

        /// <summary>
        /// 获取 URL 上的参数存入当页面 控件中。
        /// </summary>
        private void GetUrlQueryToControl()
        {
            PageHelper.GetUrlQueryToControl(this.Page);
        }

        /// <summary>
        /// 生成查询条件字符串
        /// </summary>
        private StringBuilder GenerationSQLString()
        {
            StringBuilder sql = new StringBuilder();
            #region MyRegion
            if (!String.IsNullOrEmpty(Request.QueryString["DEALERID"]))
            {
                sql.Append(" and DEALERID like '%" + Request.QueryString["DEALERID"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["DEALERNAME"]))
            {
                sql.Append(" and DEALERNAME like '%" + Request.QueryString["DEALERNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["DEALERCODE"]))
            {
                sql.Append(" and DEALERCODE like '%" + Request.QueryString["DEALERCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["MARKETDEPARTMENTID"]))
            {
                sql.Append(" and MARKETDEPARTMENTID = '" + Request.QueryString["MARKETDEPARTMENTID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["MARKETDEPARTMENTCODE"]))
            {
                sql.Append(" and MARKETDEPARTMENTCODE like '%" + Request.QueryString["MARKETDEPARTMENTCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["MARKETDEPARTMENTNAME"]))
            {
                sql.Append(" and MARKETDEPARTMENTNAME like '%" + Request.QueryString["MARKETDEPARTMENTNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["DEPLOYNAME"]))
            {
                sql.Append(" and DEPLOYNAME like '%" + Request.QueryString["DEPLOYNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CATEGORY"]))
            {
                sql.Append(" and CATEGORY like '%" + Request.QueryString["CATEGORY"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CORPTYPE"]))
            {
                sql.Append(" and CORPTYPE like '%" + Request.QueryString["CORPTYPE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CENTCORP"]))
            {
                sql.Append(" and CENTCORP like '%" + Request.QueryString["CENTCORP"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["TEMPLATENAME"]))
            {
                sql.Append(" and TEMPLATENAME like '%" + Request.QueryString["TEMPLATENAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["DISTRICT"]))
            {
                sql.Append(" and DISTRICT like '%" + Request.QueryString["DISTRICT"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["PROVINCE"]))
            {
                sql.Append(" and PROVINCE like '%" + Request.QueryString["PROVINCE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CITY"]))
            {
                sql.Append(" and CITY like '%" + Request.QueryString["CITY"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["COUNTY"]))
            {
                sql.Append(" and COUNTY like '%" + Request.QueryString["COUNTY"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BANKNAME"]))
            {
                sql.Append(" and BANKNAME like '%" + Request.QueryString["BANKNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BANKACCOUNT"]))
            {
                sql.Append(" and BANKACCOUNT like '%" + Request.QueryString["BANKACCOUNT"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["TAXCODE"]))
            {
                sql.Append(" and TAXCODE like '%" + Request.QueryString["TAXCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["PHONE"]))
            {
                sql.Append(" and PHONE like '%" + Request.QueryString["PHONE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ADDRESS"]))
            {
                sql.Append(" and ADDRESS like '%" + Request.QueryString["ADDRESS"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["POSTCODE"]))
            {
                sql.Append(" and POSTCODE like '%" + Request.QueryString["POSTCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["EMAIL"]))
            {
                sql.Append(" and EMAIL like '%" + Request.QueryString["EMAIL"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ABBR"]))
            {
                sql.Append(" and ABBR like '%" + Request.QueryString["ABBR"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["PYCODE"]))
            {
                sql.Append(" and PYCODE like '%" + Request.QueryString["PYCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["PRESIDENT"]))
            {
                sql.Append(" and PRESIDENT like '%" + Request.QueryString["PRESIDENT"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["PRESIDENTPHONE"]))
            {
                sql.Append(" and PRESIDENTPHONE like '%" + Request.QueryString["PRESIDENTPHONE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LINKMAN"]))
            {
                sql.Append(" and LINKMAN like '%" + Request.QueryString["LINKMAN"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LINKPHONE"]))
            {
                sql.Append(" and LINKPHONE like '%" + Request.QueryString["LINKPHONE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["WORKPHONE"]))
            {
                sql.Append(" and WORKPHONE like '%" + Request.QueryString["WORKPHONE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["MTLIVCCFT"]))
            {
                sql.Append(" and MTLIVCCFT like '%" + Request.QueryString["MTLIVCCFT"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["MTLIVCKIND"]))
            {
                sql.Append(" and MTLIVCKIND like '%" + Request.QueryString["MTLIVCKIND"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["TAXRATE"]))
            {
                sql.Append(" and TAXRATE like '%" + Request.QueryString["TAXRATE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["WRKIVCKIND"]))
            {
                sql.Append(" and WRKIVCKIND like '%" + Request.QueryString["WRKIVCKIND"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["WRKIVCCFT"]))
            {
                sql.Append(" and WRKIVCCFT like '%" + Request.QueryString["WRKIVCCFT"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["INVOICEMAXFEE"]))
            {
                sql.Append(" and INVOICEMAXFEE like '%" + Request.QueryString["INVOICEMAXFEE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["WRKLEVEL"]))
            {
                sql.Append(" and WRKLEVEL like '%" + Request.QueryString["WRKLEVEL"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["MNGLEVEL"]))
            {
                sql.Append(" and MNGLEVEL like '%" + Request.QueryString["MNGLEVEL"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["OLDMANAGERATE"]))
            {
                sql.Append(" and OLDMANAGERATE like '%" + Request.QueryString["OLDMANAGERATE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["WCFLEVEL"]))
            {
                sql.Append(" and WCFLEVEL like '%" + Request.QueryString["WCFLEVEL"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["SETUPTIMEBegin"]))
            {
                sql.Append(" and SETUPTIME >= '" + Request.QueryString["SETUPTIMEBegin"].Trim() + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["SETUPTIMEEnd"]))
            {
                sql.Append(" and SETUPTIME < '" + Request.QueryString["SETUPTIMEEnd"].Trim() + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["REMOVALTIMEBegin"]))
            {
                sql.Append(" and REMOVALTIME >= '" + Request.QueryString["REMOVALTIMEBegin"].Trim() + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["REMOVALTIMEEnd"]))
            {
                sql.Append(" and REMOVALTIME < '" + Request.QueryString["REMOVALTIMEEnd"].Trim() + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["FAX"]))
            {
                sql.Append(" and FAX like '%" + Request.QueryString["FAX"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["STATUS"]))
            {
                sql.Append(" and STATUS like '%" + Request.QueryString["STATUS"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CREATETIMEBegin"]))
            {
                sql.Append(" and " + bll._dal.GetBeginDateString("CREATETIME", Request.QueryString["CREATETIMEBegin"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CREATETIMEEnd"]))
            {
                sql.Append(" and " + bll._dal.GetBeginDateString("CREATETIME", Request.QueryString["CREATETIMEEnd"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CREATEPEOPLEID"]))
            {
                sql.Append(" and CREATEPEOPLEID = '" + Request.QueryString["CREATEPEOPLEID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CREATEPEOPLE"]))
            {
                sql.Append(" and CREATEPEOPLE like '%" + Request.QueryString["CREATEPEOPLE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTMODIFYPEOPLEID"]))
            {
                sql.Append(" and LASTMODIFYPEOPLEID = '" + Request.QueryString["LASTMODIFYPEOPLEID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTMODIFYPEOPLE"]))
            {
                sql.Append(" and LASTMODIFYPEOPLE like '%" + Request.QueryString["LASTMODIFYPEOPLE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTMODIFYTIMEBegin"]))
            {
                sql.Append(" and " + bll._dal.GetBeginDateString("LASTMODIFYTIME", Request.QueryString["LASTMODIFYTIMEBegin"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTMODIFYTIMEEnd"]))
            {
                sql.Append(" and " + bll._dal.GetBeginDateString("LASTMODIFYTIME", Request.QueryString["LASTMODIFYTIMEEnd"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["REMARK"]))
            {
                sql.Append(" and REMARK like '%" + Request.QueryString["REMARK"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BUSINESSDIVISIONID"]))
            {
                sql.Append(" and BUSINESSDIVISIONID = '" + Request.QueryString["BUSINESSDIVISIONID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BUSINESSDIVISIONNAME"]))
            {
                sql.Append(" and BUSINESSDIVISIONNAME like '%" + Request.QueryString["BUSINESSDIVISIONNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BUSINESSDIVISIONCODE"]))
            {
                sql.Append(" and BUSINESSDIVISIONCODE like '%" + Request.QueryString["BUSINESSDIVISIONCODE"] + "%'");
            } 
            #endregion

            if (!UserLoginInfo.IsAdministrator())
            {
                sql.Append(" and BUSINESSDIVISIONCODE = '" + _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE.Trim() + "'");
            }

            return sql;

        }

        /// <summary>
        /// 页面数据绑定
        /// </summary>
        private void gvDataBinding()
        {
            #region 查询条件字符串
            StringBuilder sql = GenerationSQLString();
            #endregion

            List<T_DEALER_Entity> list;

            //获取总的记录条数
            gvList.RecordCount = (int)bll.GetCount(sql.ToString());

            list = bll.GetList(sql.ToString(), gvList.CurrentPageIndex * gvList.PageSize, gvList.PageSize, gvList.SortConditions);

            gvList.DataSource = list;
            gvList.DataKeyNames = new string[] { "DEALERID" };
            gvList.DataBind();

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string url = HttpContext.Current.Request.Url.AbsolutePath;
            PageHelper.GetControlToUrlQuery(this.Page, ref url);
            Response.Redirect(url);
        }

        #region GridView 事件
        /// <summary>
        /// GridView 删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            T_DEALER_Entity entity = new T_DEALER_Entity();
            entity.DEALERID = StringHelper.ToInt((sender as GridView).DataKeys[e.RowIndex].Values[0].ToString());

            long rel = bll.Delete(entity);
            if (rel > 0)
            {
                Response.Redirect(HttpContext.Current.Request.RawUrl);
            }
            else
            {

            }

        }

        /// <summary>
        /// GridView 行数据绑定事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            string temp = string.Empty;
            switch (e.Row.RowType)
            {
                case DataControlRowType.DataRow:
                    {
                        GridView gv = (sender as GridView);
                        for (int i = 0; i < gv.Columns.Count; i++)
                        {
                            TemplateField tf = gv.Columns[i] as TemplateField;
                            if (tf != null)
                            {
                                #region 存储值 转换成  显示值
                                switch (tf.HeaderText)
                                {
                                    case "操作":
                                        {
                                            e.Row.Cells[i].Attributes.Add("style", "white-space: nowrap;");
                                        }
                                        break;
                                    default:
                                        {
                                            e.Row.Cells[i].Attributes.Add("style", "white-space: nowrap;");
                                        }
                                        break;

                                }
                                #endregion
                            }

                            BoundField bf = gv.Columns[i] as BoundField;
                            if (bf != null)
                            {
                                #region 存储值 转换成  显示值
                                switch (bf.DataField)
                                {
                                    case "STATUS":
                                        {
                                            temp = e.Row.Cells[i].Text.Trim();
                                            temp = ((Status)Enum.Parse(typeof(Status), temp)).ToString();
                                            e.Row.Cells[i].Text = temp;
                                        }
                                        break;
                                    default:
                                        {

                                        }
                                        break;
                                }
                                #endregion
                            }
                        }
                    }
                    break;
                case DataControlRowType.Header:
                    {

                    }
                    break;
                case DataControlRowType.Pager:
                    {

                    }
                    break;
                case DataControlRowType.EmptyDataRow:
                    {


                    }
                    break;
                default:

                    break;
            }

        }

        /// <summary>
        /// GridView 行创建事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowCreated(object sender, GridViewRowEventArgs e)
        {
            switch (e.Row.RowType)
            {
                case DataControlRowType.DataRow:
                    {

                    }
                    break;
                case DataControlRowType.Header:
                    {

                    }
                    break;
                case DataControlRowType.Pager:
                    {

                    }
                    break;
                case DataControlRowType.EmptyDataRow:
                    {


                    }
                    break;
                default:

                    break;
            }

        }
        #endregion
    }
}
